package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.qiaoyanqing.ximalaya.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DiscoverRankPlayActivity extends AppCompatActivity {
    private List<String> url;
    private List<String> title;
    private MediaPlayer player=new MediaPlayer();
    private boolean pause;
    private int position=0;
    private ImageView next;
    private ImageView last;
    private boolean nextIsAble;
    private boolean lastIsAble;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discover_rank_play);
        initView();
    }

    private void initView() {

        url=new ArrayList<>();
        title=new ArrayList<>();
        Intent intent=getIntent();
        url=intent.getStringArrayListExtra("url");
        title=intent.getStringArrayListExtra("title");
        position = intent.getIntExtra("position",0);
        play();
        next=(ImageView)findViewById(R.id.play_next);
        next.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if(position<url.size()){
                            next.setImageResource(R.drawable.player_toolbar_next_pressed);

                        }else {
                            Toast.makeText(DiscoverRankPlayActivity.this,"没有更多了,试试别的吧",Toast.LENGTH_LONG).show();

                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_UP:
                        if(position==0){
                            last.setImageResource(R.drawable.player_toolbar_previous_normal);
                        }
                        next.setImageResource(R.drawable.player_toolbar_next_normal);
                        next(view);
                }
                return false;
            }
        });
        last=(ImageView)findViewById(R.id.play_last);
        last.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if(position>0){
                            last.setImageResource(R.drawable.player_toolbar_previous_pressed);
                        }else {
                            Toast.makeText(DiscoverRankPlayActivity.this,"已经是第一首了,试试别的吧",Toast.LENGTH_LONG).show();
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                    case MotionEvent.ACTION_UP:
                        if(position==url.size()){
                            next.setImageResource(R.drawable.player_toolbar_next_normal);
                        }
                        last.setImageResource(R.drawable.player_toolbar_previous_normal);
                        last(view);
                }
                return false;
            }
        });
    }

    public void onClick(View view){
        Toast.makeText(this,"主播详情",Toast.LENGTH_SHORT).show();
    }
    public void stop(){
        if (player!=null&&player.isPlaying()){
            player.stop();
        }
    }
    public boolean pause(View view){
        if(player.isPlaying()){
            player.pause();
            pause=true;
        }else {
            if(pause){
                player.start();
                pause=false;
            }
        }
        return pause;
    }
    public void next(View view){
        if(position<url.size()){
            player.reset();
            position++;
            play();
        }else {
            next.setImageResource(R.drawable.player_toolbar_next_disable);
        }
    }

    public void last(View view){
        if(position>0){
            player.reset();
            position--;
            play();
        }else {
            last.setImageResource(R.drawable.player_toolbar_previous_disable);
        }

    }
    public void play(){
        try {
            player.setDataSource(url.get(position));
            player.prepare();
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
